﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight;
using System.Diagnostics;
using GalaSoft.MvvmLight.Command;
using QConnectSDK.Models;
using QConnectSDK.Api;
using QConnectSDK.Context;
using QConnectSDK.Exceptions;

namespace OpenConnect.WPSample.ViewModel
{
    public class MainViewModel : ViewModelBase
    {

        private User _profile;
        private IAuthProvider _authProvider;

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel(IAuthProvider authProvider)
        {
            Debug.Assert(authProvider != null);
            _authProvider = authProvider;

            if (IsInDesignMode)
            {
                
            }

            RefreshCommand = new RelayCommand(Refresh);
            LogoutCommand = new RelayCommand(Logout);
        }

        public ICommand RefreshCommand { get; private set; }
        public ICommand LogoutCommand { get; private set; }

        private void Logout()
        {
            _authProvider.Logout();       

        }

        private void Refresh()
        {
            _authProvider.GetAccessCode((s, t) => LoadProfile(s, t));
        }

        private void AddFeeds(string access_token, string openId)
        {
            OAuthToken token = new OAuthToken()
            {
                AccessToken = access_token,
                OpenId = openId
            };
            QzoneContext context = new QzoneContext(token);
            RestApi restapi = new RestApi(context);
            restapi.AddFeedsAsync(AddFeedsSucess, AddFeedsFailed, "test from wp7", "http://www.win8charm.com");
        }

        private void AddFeedsSucess(QzoneBase content)
        {

        }

        private void AddFeedsFailed(QzoneException ex)
        {
            Debug.Assert(ex != null);
        }


        public string ApplicationTitle
        {
            get
            {
                return ViewModelLocator.AppName;
            }
        }

        public string PageName
        {
            get
            {
                return "用户信息";
            }
        }

        public User Profile
        {
            get
            {
                if (_profile == null)
                    _authProvider.GetAccessCode((s,t) => LoadProfile(s,t));

                return _profile;
            }
            set
            {
                if (_profile != value)
                {
                    _profile = value;
                    RaisePropertyChanged("Profile");
                }
            }

        }

        private bool _loading;

        private void LoadProfile(string access_token,string openId)
        {
            if (_profile == null && !_loading)
            {
                Debug.WriteLine("loading profile");

                OAuthToken token = new OAuthToken()
                {
                    AccessToken = access_token,
                     OpenId = openId
                };
                QzoneContext context = new QzoneContext(token);
                RestApi restapi = new RestApi(context);
                restapi.GetCurrentUserAsync(ProfileLoaded, ProfileLoadedFailed);
                
            }
        }

        private void ProfileLoaded(User user)
        {
            Profile = user;
        }

        private void ProfileLoadedFailed(QzoneException ex)
        {
            
        }
    }
}
